package xyz.lne.researchgroupmanage.controller;


import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import xyz.lne.researchgroupmanage.annotation.AnonymousGetMapping;
import xyz.lne.researchgroupmanage.annotation.AnonymousPostMapping;
import xyz.lne.researchgroupmanage.controller.request.UserRequest;
import xyz.lne.researchgroupmanage.controller.response.UserResponse;
import xyz.lne.researchgroupmanage.service.ResearchService;
import xyz.lne.researchgroupmanage.service.ResearchGroupService;
import xyz.lne.researchgroupmanage.service.SysUserService;
import xyz.lne.researchgroupmanage.service.TaskService;
import xyz.lne.researchgroupmanage.service.dataTransferObject.UserDTO;
import xyz.lne.researchgroupmanage.util.SecurityUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author team7
 * @since 2021-01-19
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/sys-user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private ResearchService researchService;

    @Autowired
    private ResearchGroupService researchgroupService;

    @Autowired
    private TaskService taskService;

    private final PasswordEncoder passwordEncoder;

    @AnonymousGetMapping("/findUser")
    public UserResponse findUserById(){
        //TODO: need convert to response
        UserDTO userDTO=sysUserService.findUserById(SecurityUtils.getCurrentUserId());
        return userDTO2UserResponse(userDTO);
    }

    @AnonymousGetMapping("/findUserById")
    public UserResponse findOtherUser(Integer userId){
        sysUserService.isLegal(userId);
        UserDTO userDTO=sysUserService.findUserById(userId);
        return userDTO2UserResponse(userDTO);
    }

    @AnonymousPostMapping("/addUser")
    public UserResponse addUser(@RequestBody UserRequest userRequest){
        userRequest.setPassword(passwordEncoder.encode(userRequest.getPassword()));
        return userDTO2UserResponse(sysUserService.addUser(userRequest2UserDTO(userRequest)));
    }

    @AnonymousPostMapping("/update")
    public UserResponse updateUser(@RequestBody UserRequest userRequest){
        UserDTO userDTO=userRequest2UserDTO(userRequest);
        userDTO.setUserId(SecurityUtils.getCurrentUserId());
        sysUserService.updateUser(userDTO);
        return userDTO2UserResponse(userDTO);
    }

    @AnonymousGetMapping("/listAll")
    public List<UserResponse> listAll(){
        List<UserDTO> list=sysUserService.listAll();
        List<UserResponse> userResponseList=new ArrayList<>();

        for(UserDTO userDTO:list){
            userResponseList.add(userDTO2UserResponse(userDTO));
        }
        return userResponseList;
    }

    @AnonymousGetMapping("/testAccountAvailability")
    public Boolean isAccountAvailable(String account){
        return sysUserService.isAccountAvailable(account);
    }


    private UserResponse userDTO2UserResponse(UserDTO userDTO){
        UserResponse userResponse=new UserResponse();
        userResponse.setUserId(userDTO.getUserId());
        userResponse.setAccount(userDTO.getAccount());
        userResponse.setLevel(userDTO.getLevel());
        userResponse.setPassword(userDTO.getPassword());
        userResponse.setEmail(userDTO.getEmail());
        userResponse.setName(userDTO.getName());
        userResponse.setPhoneNumber(userDTO.getPhoneNumber());
        userResponse.setSex(userDTO.getSex());
        userResponse.setBirthday(userDTO.getBirthday());
        userResponse.setCompany(userDTO.getCompany());
        userResponse.setPosition(userDTO.getPosition());
        userResponse.setIntroduction(userDTO.getIntroduction());
        userResponse.setUserImg(userDTO.getUserImg());

        //TODO: 需要课题组模块提供相应的接口
        userResponse.setResearchGroupManageSimpleDTOList(researchgroupService.findCreatedResearchGroupByUserId(userDTO.getUserId()));
        userResponse.setResearchGroupJoinSimpleDTOList(researchgroupService.findJoinedResearchGroupByUserId(userDTO.getUserId()));

        userResponse.setResearchJoinSimpleDTOList(researchService.findResearchByuserId(userDTO.getUserId()));
        userResponse.setResearchManageSimpleDTOList(researchService.findResearchBymanagerId(userDTO.getUserId()));

        userResponse.setTaskReleaseSimpleDTOList(taskService.getReleasedTaskListByUserId(userDTO.getUserId()));
        userResponse.setTaskAcceptSimpleDTOList(taskService.getParticipatedTaskListByUserId(userDTO.getUserId()));

        return userResponse;
    }

    private UserDTO userRequest2UserDTO(UserRequest userRequest){
        UserDTO userDTO=new UserDTO();
        userDTO.setLevel(0);
        userDTO.setAccount(userRequest.getAccount());
        userDTO.setPassword(userRequest.getPassword());
        userDTO.setEmail(userRequest.getEmail());
        userDTO.setName(userRequest.getName());
        userDTO.setPhoneNumber(userRequest.getPhoneNumber());
        userDTO.setSex(userRequest.getSex());
        userDTO.setBirthday(userRequest.getBirthday());
        userDTO.setCompany(userRequest.getCompany());
        userDTO.setPosition(userRequest.getPosition());
        userDTO.setIntroduction(userRequest.getIntroduction());
        userDTO.setUserImg(userRequest.getUserImg());
        userDTO.setCreateDate(new Date());
        userDTO.setIsDeleted(0);

        return userDTO;
    }
}

